<%--
  Created by IntelliJ IDEA.
  User: dusy
  Date: 2017/12/18
  Time: 11:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>代录单人注册隐性客户</title>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript">
        var comInfoId = "${companyInfo.comInfoId}";
        var agentRefId = "${companyInfo.agentRefId}";
        var auditStatus = "${companyInfo.auditStatus}"
        var customerFormSetting = {
            title: "新注册客户信息",
            module: 'save',
            columns: [{
                name: "id",
                hidden: true,
                value: "${companyInfo.comInfoId}"
            }, {
                name: "name",
                label: "公司名称",
                validate: 'validate[required]',
                value: "${companyInfo.name}"
            }, {
                name: "uniteCode",
                label: "统一社会信用代码",
                validate: 'validate[required]',
                value: "${companyInfo.uniteCode}"
            }, {
                name: "customsCode",
                label: "海关企业注册代码",
                value: "${companyInfo.customsCode}"
            }, {
                name: "address",
                label: "公司地址",
                value: "${companyInfo.address}"
            }],
            buttons: {
                custom: [{
                    id: "newContact",
                    value: "新增联系人",
                    click: function () {
                        add();
                    }
                }]
            },
            form: {
                'url': 'saveOrUpdate',
                'ajaxForm': {
                    success: function (data) {
                        if (data.result) {
                            comInfoId = data.data.comInfoId;
                            location.href =
                                "/order/tacitCustomerRegistration?comInfoId=" + comInfoId;
                        } else {
                            jw.alert(data.msg);
                        }
                    }
                }
            }
        };
        var contactGridSetting = {
            grid: {
                id: "contactGrid",
                title: "联系人信息管理",
                url: "/tGenComContact/getContact?comInfoId=" + comInfoId,
                colModel: [{
                    index: "COM_CONTACT_ID",
                    name: "comContactId",
                    hidden: true,
                    key: true
                }, {
                    index: "name",
                    name: "name",
                    label: "姓名"
                }, {
                    index: "dept",
                    name: "dept",
                    label: "部门"
                }, {
                    index: "position",
                    name: "position",
                    label: "职务"
                }, {
                    index: "telPhone",
                    name: "telPhone",
                    label: "联系电话"
                }, {
                    index: "email",
                    name: "email",
                    label: "邮箱"
                }, {
                    index: "qq",
                    name: "qq",
                    label: "QQ"
                }, {
                    label: "操作",
                    sortable: false,
                    buttons: [{
                        key: "id2",
                        value: "修改",
                        click: function (gridid, rowId, rowdata) {
                            modify(rowId);
                        }
                    }, {
                        key: "id3",
                        value: "删除",
                        click: function (gridid, rowId, rowdata) {
                            del(rowId);
                        }
                    }],
                    formatter: $.jqGrid.custom.formatter.button
                }]
            }
        };
        var attachmentGridSetting = {
            grid: {
                id: "attachmentGrid",
                title: "已提交附件",
                url: "/order/getAttachment?comInfoId=" + comInfoId,
                colModel: [{
                    index: "name",
                    name: "name",
                    label: "文件名",
                    formatter: function (val, opts, row) {
                        if (val) {
                            return "<a href='javascript:downloadAtta(\"" + row.id + "\")'>"
                                   + val + "</a>"
                        } else {
                            return "";
                        }
                    }
                }, {
                    index: "ctime",
                    name: "ctime",
                    label: "上传时间"
                }, {
                    label: "操作",
                    sortable: false,
                    buttons: [{
                        key: "attachmentDelBtn",
                        value: "删除",
                        click: function (gridid, rowId, rowdata) {
                            $.ajax({
                                       url: "attachmentDel",
                                       cache: false,
                                       type: 'post',
                                       data: {"id": rowId},
                                       success: function (data) {
                                           if (data.result) {
                                               reloadAttachment();
                                           }
                                           jw.alert(data.msg);
                                       }
                                   });
                        }
                    }],
                    formatter: $.jqGrid.custom.formatter.button
                }]
            }
        };

        function moveBtn() {
            var btnTr = $("tr.mrdBtnTr");
            var btnContainer = btnTr.find("td#mf_buttons_container");
            btnContainer.attr("colspan", 6);
            btnContainer.css("text-align", "right");
        }

        $(document).ready(function () {
            if (comInfoId) {
                $("#attachmentInfo").mrdForm(attachmentGridSetting);
                $("#contactPerson").mrdForm(contactGridSetting);
            }
            if (comInfoId) {
                showPage();
            } else {
                hidePage();
                customerFormSetting.buttons = {};
            }
            if (auditStatus == '0' || auditStatus == '1') {
                for (var i = 0; i < customerFormSetting.columns.length; i++) {
                    customerFormSetting.columns[i].readonly = true;
                }
                $("#attachment").hide();
            }
            $("#customerInfo").mrdForm(customerFormSetting);
            if (auditStatus == '0' || auditStatus == '1') {
                $("#saveButton").hide();
                $("#submitBtn").hide();
            }
            $("#form_in_control_3").bind("blur", function () {
                if ($("#form_in_control_3").val().trim().length != 18) {
                    layer.msg("统一社会信用代码必须为18位");
                } else {//根据统一社会信用代码自动填充其他数据
                    backfill();
                }
            });
        });

        /**
         * 根据统一社会信用代码自动填充其他数据
         */
        function backfill() {
            var uniteCode = $("#form_in_control_3").val().trim();
            if (uniteCode.length == 18) {
                $.ajax({
                           url: "/order/getCompInfo",
                           type: "post",
                           data: {"uniteCode": uniteCode},
                           cache: false,
                           success: function (companyInfo) {
                               if (companyInfo.comInfoId) {
                                   comInfoId = companyInfo.comInfoId;
                                   $("#form_in_control_2").val(companyInfo.name);
                                   $("#form_in_control_4").val(companyInfo.customsCode);
                                   $("#form_in_control_5").val(companyInfo.address);
                                   setReadonly($("#form_in_control_2"));
                                   setReadonly($("#form_in_control_4"));
                                   setReadonly($("#form_in_control_5"));
                               } else {
                                   if (comInfoId) {
                                       cancleReadonly($("#form_in_control_2"));
                                       cancleReadonly($("#form_in_control_4"));
                                       cancleReadonly($("#form_in_control_5"));
                                       $("#form_in_control_2").val("");
                                       $("#form_in_control_4").val("");
                                       $("#form_in_control_5").val("");
                                       comInfoId = "";
                                   }
                               }
                           }
                       });
            }
        }

        function setReadonly(obj) {
            obj.attr("readonly", true);
            obj.css({"background-color": "rgb(235, 235, 228)"});
        }

        function cancleReadonly(obj) {
            obj.attr("readonly", false);
            obj.css({"background-color": ""});
        }

        function showPage() {
            $("#attachment").show();
            $("#contactPerson").show();
            $("#others").show();
            $("#newContact").show();
            $("form#attachmentUpload").submit(function (e) {
                $("#comInfoId").val(comInfoId);
                e.preventDefault();
                var formData = new FormData($(this)[0]);
                var index = layer.load(1, {
                    shade: [0.1, '#000'] //0.1透明度的白色背景
                });
                $.ajax({
                           url: $(this).attr('action'), // le nom du fichier indiqué dans le formulaire
                           type: $(this).attr('method'), // la méthode indiquée dans le formulaire (get ou post)
                           cache: false,
                           data: formData,
                           contentType: false,
                           processData: false,
                           success: function (data) {
                               layer.close(index);
                               jw.alert(data.msg);
                               if (data.result) {
                                   reloadAttachment();
                               }
                           },
                           error: function (XMLHttpRequest, msg, e2) {
                               layer.close(index);
                               jw.alert('文件大小不要超过10M。');
                           }
                       });
                return false;
            });
        }

        function hidePage() {
            $("#attachment").hide();
            $("#contactPerson").hide();
            $(".others").hide();
            $("#newContact").hide();
        }

        function goBack() {
            location.href = "/getCusInfo/cusInfoList";
        }

        function reloadAttachment() {
            $("#attachmentGrid").jqGrid("getGridParam", "postData").filters = "";
            $("#attachmentGrid").jqGrid().trigger("reloadGrid");
        }

        function reloadContact() {
            $("#contactGrid").jqGrid("getGridParam", "postData").filters = "";
            $("#contactGrid").jqGrid().trigger("reloadGrid");
        }

        function downloadAtta(id) {
            var url = "/order/downloadComAttachment?id=" + id;
            $.download(url);
        }

        function recessiveComSubmit() {
            jw.confirm("提交后数据将不能更改，确认提交？", function () {
                $.ajax({
                           url: "/order/recessiveComSubmit",
                           type: "post",
                           data: {"comInfoId": comInfoId},
                           cache: false,
                           success: function (data) {
                               if (data.result) {
                                   $("#submitBtn").hide();
                                   location.href = "/getCusInfo/cusInfoList";
                               } else {
                                   jw.alert(data.msg);
                               }
                           }
                       });
            }, function () {
                return;
            })

        }

        /**
         * 新增
         * @author han
         */
        function add() {
            clearForm();
            layer.open({
                           type: 1,
                           area: ['600px', '400px'],
                           title: '新增联系人',
                           maxmin: true, //开启最大化最小化按钮
                           content: $("#winContent"),
                           btn: ['确定', '关闭'],
                           yes: function (index, layero) {
                               var param = {};
                               param.comContactId = $('#comContactId').val();
                               param.name = $('#name').val();
                               param.telPhone = $('#telPhone').val();
                               param.email = $('#email').val();
                               param.dept = $('#dept').val();
                               param.position = $('#position').val();
                               param.qq = $('#qq').val();
                               param.arefId = agentRefId;
                               var url = "/tGenComContact/save";

                               $.ajax({
                                          type: "POST",
                                          url: url,
                                          data: param,
                                          success: function (r) {
                                              layer.alert(r.error_code);
                                              if (r.error_code == 0) {
                                                  reloadContact();
                                              } else {
                                                  layer.alert(r.msg);
                                              }
                                              layer.closeAll();
                                          }
                                      });
                           },
                           cancel: function (index) {
                               clearForm();
                           }
                       });
        }

        /**
         * 修改
         * @author han
         */
        function modify(id) {
            if (id == null) {
                return;
            }
            var row = $("#contactGrid").getRowData(id);

            $('#comContactId').val(row.comContactId);
            $('#name').val(row.name);
            $('#telPhone').val(row.telPhone);
            $('#email').val(row.email);
            $('#dept').val(row.dept);
            $('#position').val(row.position);
            $('#qq').val(row.qq);

            layer.open({
                           type: 1,
                           area: ['600px', '400px'],
                           title: '修改联系人',
                           maxmin: true, //开启最大化最小化按钮
                           content: $("#winContent"),
                           btn: ['确定', '关闭'],
                           yes: function (index, layero) {
                               var param = {};
                               param.comContactId = $('#comContactId').val();
                               param.name = $('#name').val();
                               param.telPhone = $('#telPhone').val();
                               param.email = $('#email').val();
                               param.dept = $('#dept').val();
                               param.position = $('#position').val();
                               param.qq = $('#qq').val();
                               param.arefId = agentRefId;
                               var url = "/tGenComContact/update";

                               $.ajax({
                                          type: "POST",
                                          url: url,
                                          data: param,
                                          success: function (r) {
                                              if (r.error_code == 0) {
                                                  $("#contactGrid").trigger("reloadGrid");
                                              } else {
                                                  layer.alert(r.msg);
                                              }
                                              layer.closeAll();
                                          }
                                      });
                           },
                           cancel: function (index) {
                               clearForm();
                           }
                       });
        }

        /**
         * 删除
         * @author han
         */
        function del(id) {
            var ids = [id];
            if (ids == null) {
                return;
            }
            $.ajax({
                       type: "POST",
                       url: "/tGenComContact/delete",
                       contentType: "application/json",
                       data: JSON.stringify(ids),
                       success: function (r) {
                           if (r.error_code == 0) {
                               layer.alert("删除成功");
                               $("#contactGrid").trigger("reloadGrid");
                           } else {
                               layer.alert(r.msg);
                           }
                       }
                   });
        }

        /**
         * 弹出窗口初始化
         * @author han
         **/
        function clearForm() {
            $('#comContactId').val("");
            $('#name').val("");
            $('#telPhone').val("");
            $('#email').val("");
            $('#dept').val("");
            $('#position').val("");
            $('#qq').val("");
        }
    </script>
</head>
<body>
<div id="customerInfo"></div>
<div id="contactPerson"></div>
<div id="attachment">
    <div id="attachmentInfo"></div>
    <br/>
    <div align="left" class="others">
        <form action="uploadCompanyAttachment" enctype="multipart/form-data" method="post"
              id="attachmentUpload">
            <input style="padding-left: 60px" type="file" name="file" required/>
            <input hidden="true" type="text" name="comInfoId" id="comInfoId" value="123"/>
            <input class="ui-button ui-widget ui-state-default ui-corner-all" type="submit"
                   value="上传"/>
            <p align="left" style="padding-left: 60px">注：文件大小不要超过10M。</p>
        </form>
    </div>
</div>
<div id="others">
    <br/>
    <div align="center">
        <input type="button" class="ui-corner-all ui-button" value="返回" onclick="goBack()"/>
        <input type="button" class="others ui-button ui-widget ui-state-default ui-corner-all"
               value="提交" id="submitBtn" onclick="recessiveComSubmit()"/>
    </div>
</div>
<div id="winContent" style="display:none;margin:5px 5px 5px 5px;">
    <table width="100%" border="1" cellpadding="1" bordercolor="#eeeeee">
        <tr bgcolor="#FFFFFF">
            <td width="198" height="38" align="right" bgcolor="#f8f8f8">姓名：</td>
            <td width="345" align="left">
                <input type="hidden" id="comContactId" name="comContactId">
                <input id="name" name="name" size="45"/>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td width="198" height="38" align="right" bgcolor="#f8f8f8">部门：</td>
            <td width="345" align="left">
                <input id="dept" name="dept" size="45"/>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td width="198" height="38" align="right" bgcolor="#f8f8f8">职务：</td>
            <td width="345" align="left">
                <input id="position" name="position" size="45"/>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td width="198" height="38" align="right" bgcolor="#f8f8f8">联系电话：</td>
            <td width="345" align="left">
                <input id="telPhone" name="telPhone" size="45"/>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td align="right" height="38" bgcolor="#f8f8f8">邮箱：</td>
            <td align="left">
                <input id="email" name="email" size="45"/>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td align="right" height="38" bgcolor="#f8f8f8">QQ：</td>
            <td align="left">
                <input id="qq" name="qq" size="45"/>
            </td>
        </tr>
    </table>
</div>
</body>
</html>
